# ~*~ coding: utf-8 ~*~
from flask_wtf import FlaskForm
from wtforms import StringField

from apps.common.form import ErrorFormMinx

__all__ = ['RoleForm', 'RoleExecuteForm']


class RoleForm(ErrorFormMinx, FlaskForm):
    name = StringField()
    code = StringField()
    desc = StringField()

    @property
    def data(self):
        return {
            'name': self.name.data,
            'code': self.code.data,
            'desc': self.desc.data,
        }


class RoleExecuteForm(ErrorFormMinx, FlaskForm):
    roles = StringField()
    hosts = StringField()
    groups = StringField()

    def validate(self):
        if not super().validate():
            return False

        if not self.groups.data and not self.hosts.data:
            self.update_error(
                {
                    'global': ['请选择主机组或者主机']
                }
            )
            return False

        return True

    @property
    def data(self):
        return {
            'roles': self.roles.data,
            'hosts': self.hosts.data,
            'groups': self.groups.data
        }
